<?php

function formula_rapel_umum($rapel_id , $str){

	$periode_gaji_id =get_periode_gaji(); 
	$formulas = array();
	$formulas['[GAPOK_RAPEL]'] = 'get_gapok_rapel($karyawan_id , '.$periode_gaji_id.' )';
	$formulas['[KJK_RAPEL]'] = 'pr_rapel_kjk($karyawan_id , $komponen_id , $periode_id , '.$rapel_id.' , '.$periode_gaji_id.')';
	
	$formulas['[OVERTIME_OTOMATIS_RAPEL]'] = 'pr_rapel_overtime_otomatis($karyawan_id , $komponen_id , $periode_id , '.$rapel_id.' , '.$periode_gaji_id.')';
	$formulas['[OVERTIME_RAPEL]'] = 'pr_rapel_overtime($karyawan_id , $komponen_id , $periode_id , '.$rapel_id.' , '.$periode_gaji_id.')';
	
	$formulas['[CUTI_BESAR_RAPEL]'] = 'pr_rapel_cuti_besar($karyawan_id ,$komponen_id, $periode_id , '.$rapel_id.' , '.$periode_gaji_id.')';
	$formulas['[CUTI_TAHUNAN_RAPEL]'] = 'pr_rapel_cuti_tahunan($karyawan_id ,$komponen_id, $periode_id , '.$rapel_id.' , '.$periode_gaji_id.')';
	
	$formulas['[IURAN_JHT_RAPEL]'] = 'pr_rapel_iuran_jht($karyawan_id ,$komponen_id, $periode_id , '.$rapel_id.' , '.$periode_gaji_id.')';
	$formulas['[IURAN_JK_RAPEL]'] = 'pr_rapel_iuran_jk($karyawan_id ,$komponen_id, $periode_id , '.$rapel_id.' , '.$periode_gaji_id.')';
	$formulas['[IURAN_JKK_RAPEL]'] = 'pr_rapel_iuran_jkk($karyawan_id ,$komponen_id, $periode_id , '.$rapel_id.' , '.$periode_gaji_id.')';
	$formulas['[IURAN_MITRA_KRAKATAU]'] = 'pr_rapel_iuran_mitra_krakatau($karyawan_id ,$komponen_id, $periode_id , '.$rapel_id.' , '.$periode_gaji_id.')';
	$formulas['[IURAN_TKK_BSM_RAPEL]'] = 'pr_rapel_iuran_tkk_bsm($karyawan_id ,$komponen_id, $periode_id , '.$rapel_id.' , '.$periode_gaji_id.')';
	$formulas['[IURAN_TKK_ORGANIK]'] = 'pr_rapel_iuran_tkk_organik($karyawan_id ,$komponen_id, $periode_id , '.$rapel_id.' , '.$periode_gaji_id.')';
	
	$formulas['[POTPEN_MITRA_KRAKATAU_RAPEL]'] = 'pr_rapel_potongan_mitra_krakatau($karyawan_id ,$komponen_id, $periode_id , '.$rapel_id.' , '.$periode_gaji_id.')';
	$formulas['[POT_TKK_BSM_RAPEL]'] = 'pr_rapel_potongan_tkk_bsm($karyawan_id ,$komponen_id, $periode_id , '.$rapel_id.' , '.$periode_gaji_id.')';
	$formulas['[POT_TKK_ORGANIK_RAPEL]'] = 'pr_rapel_potongan_kary_organik($karyawan_id ,$komponen_id, $periode_id , '.$rapel_id.' , '.$periode_gaji_id.')';
	
	
	$text = $str;
	foreach($formulas as $key => $val ){ 
		$text  = str_replace($key , $val , $text); 
	}
	return $text ;
}

function get_gapok_rapel($karyawan_id, $periode_gaji_id){
	$query = "SELECT a.gaji_lama,	a.gaji_baru FROM pr_kenaikan_gaji_karyawan a
	INNER JOIN pr_kenaikan_gaji b ON a.kenaikan_gaji_id = b.kenaikan_gaji_id
	INNER JOIN pr_periode c ON c.tanggal = b.tanggal
	WHERE a.karyawan_id = {$karyawan_id} AND c.periode_id = {$periode_gaji_id}";

	$result = my_query($query);
	$row = my_fetch_array($result);
	return  (round($row['gaji_baru']) - $row['gaji_lama'] );
}

function pr_rapel_kjk($karyawan_id ,$komponen_id, $periode_id , $rapel_id , $periode_gaji_id){
	 //CHECK TOTAL JAM PADA PERIODE YG DIMAKSUD
	$query = "SELECT njam FROM tmn_kjk WHERE periode_id={$periode_id} AND	karyawan_id	= {$karyawan_id} 	";
	$result = my_query($query);
	$row = my_fetch_array($result);
	$njam = $row['njam'];
	
	$rapel_gapok = get_gapok_rapel($karyawan_id, $periode_gaji_id);
	$nominal = $njam * 2 * $rapel_gapok / 173;
 
	if((int) $nominal == 0)return false;
	$datas = array(
		'rapel_id'	=>my_type_data_int($rapel_id),
		'periode_prev_id'=>my_type_data_int($periode_id),
		'komponen_id'=>my_type_data_int($komponen_id),
		'karyawan_id'=>my_type_data_int($karyawan_id),
		'nominal'=>my_type_data_str(round($nominal) ),
	); 
	return my_insert_record( 'pr_rapel_komponen_karyawan' , $datas);
}

function pr_rapel_overtime($karyawan_id ,$komponen_id, $periode_id , $rapel_id , $periode_gaji_id){
	 //CHECK TOTAL JAM PADA PERIODE YG DIMAKSUD
	$query  = "SELECT   SUM(hourpay) as thourpay FROM tmn_overtime WHERE periode_id={$periode_id} AND karyawan_id = {$karyawan_id}  ";
	$res = my_query($query);
	$rw = my_fetch_array($res); 
	$total_jam = $rw['thourpay'];
	
	$rapel_gapok = get_gapok_rapel($karyawan_id, $periode_gaji_id);
	$nominal = ( $total_jam / 173 ) * $rapel_gapok ;
	if((int) $nominal == 0)return false;
	$datas = array(
		'rapel_id'	=>my_type_data_int($rapel_id), 
		'periode_prev_id'=>my_type_data_int($periode_id),
		'komponen_id'=>my_type_data_int($komponen_id),
		'karyawan_id'=>my_type_data_int($karyawan_id),
		'nominal'=>my_type_data_str(round($nominal) ),
	); 
	return my_insert_record( 'pr_rapel_komponen_karyawan' , $datas);
}
function pr_rapel_overtime_otomatis($karyawan_id ,$komponen_id, $periode_id , $rapel_id , $periode_gaji_id){
	 //CHECK TOTAL JAM PADA PERIODE YG DIMAKSUD
	$query = "SELECT nhari FROM tmn_khk WHERE periode_id={$periode_id} AND	karyawan_id	= {$karyawan_id} 	";
	$result = my_query($query);
	$row = my_fetch_array($result);
	$nhari = $row['nhari'];
	
	$rapel_gapok = get_gapok_rapel($karyawan_id, $periode_gaji_id);
	$nominal = ( $nhari / 173 ) * $rapel_gapok ;
	if((int) $nominal == 0)return false;
	$datas = array(
		'rapel_id'	=>my_type_data_int($rapel_id),
		'periode_prev_id'=>my_type_data_int($periode_id),
		'komponen_id'=>my_type_data_int($komponen_id),
		'karyawan_id'=>my_type_data_int($karyawan_id),
		'nominal'=>my_type_data_str(round($nominal) ),
	); 
	return my_insert_record( 'pr_rapel_komponen_karyawan' , $datas);
}

/*FORMULA CUTI BESAR*/
function pr_rapel_cuti_besar($karyawan_id ,$komponen_id, $periode_id , $rapel_id , $periode_gaji_id){
	 
		$query = "SELECT kalkulasi FROM pr_gaji_komponen 
			WHERE periode_id={$periode_id} 
			AND	karyawan_id	= {$karyawan_id} 
			AND	komponen_id	= {$komponen_id} 
			";
		$result = my_query($query);
		$row = my_fetch_array($result);
		$nominal =  $row['kalkulasi']; 
		if((int) $nominal == 0)return false;
	
	$rapel_cuti = 2 * get_gapok_rapel($karyawan_id, $periode_gaji_id);
	  
	$datas = array(
		'rapel_id'	=>my_type_data_int($rapel_id),
		'periode_prev_id'=>my_type_data_int($periode_id),
		'komponen_id'=>my_type_data_int($komponen_id),
		'karyawan_id'=>my_type_data_int($karyawan_id),
		'nominal'=>my_type_data_str(round($rapel_cuti) ),
	); 
	return my_insert_record( 'pr_rapel_komponen_karyawan' , $datas);
}

/*FORMULA CUTI */
function pr_rapel_cuti_tahunan($karyawan_id ,$komponen_id, $periode_id , $rapel_id , $periode_gaji_id){
	 	
		$query = "SELECT kalkulasi FROM pr_gaji_komponen 
			WHERE periode_id={$periode_id} 
			AND	karyawan_id	= {$karyawan_id} 
			AND	komponen_id	= {$komponen_id} 
			";
		$result = my_query($query);
		$row = my_fetch_array($result);
		$nominal =  $row['kalkulasi'];
		if((int) $nominal == 0)return false;

	
	$rapel_cuti = get_gapok_rapel($karyawan_id, $periode_gaji_id);
	 
	
	$datas = array(
		'rapel_id'	=>my_type_data_int($rapel_id),
		'periode_prev_id'=>my_type_data_int($periode_id),
		'komponen_id'=>my_type_data_int($komponen_id),
		'karyawan_id'=>my_type_data_int($karyawan_id),
		'nominal'=>my_type_data_str(round($rapel_cuti) ),
	); 
	return my_insert_record( 'pr_rapel_komponen_karyawan' , $datas);
}

/* FORMULA IURAN JHT */
function pr_rapel_iuran_jht($karyawan_id ,$komponen_id, $periode_id , $rapel_id , $periode_gaji_id){
	 	
		$query = "SELECT kalkulasi FROM pr_gaji_komponen 
			WHERE periode_id={$periode_id} 
			AND	karyawan_id	= {$karyawan_id} 
			AND	komponen_id	= {$komponen_id} 
			";
		$result = my_query($query);
		$row = my_fetch_array($result);
		$nominal =  $row['kalkulasi'];
		if((int) $nominal == 0)return false;

	
	$rapel_cuti = 0.037 * get_gapok_rapel($karyawan_id, $periode_gaji_id);
	 
	
	$datas = array(
		'rapel_id'	=>my_type_data_int($rapel_id),
		'periode_prev_id'=>my_type_data_int($periode_id),
		'komponen_id'=>my_type_data_int($komponen_id),
		'karyawan_id'=>my_type_data_int($karyawan_id),
		'nominal'=>my_type_data_str(round($rapel_cuti) ),
	); 
	return my_insert_record( 'pr_rapel_komponen_karyawan' , $datas);
}
/* FORMULA IURAN JK */
function pr_rapel_iuran_jk($karyawan_id ,$komponen_id, $periode_id , $rapel_id , $periode_gaji_id){
	 	
		$query = "SELECT kalkulasi FROM pr_gaji_komponen 
			WHERE periode_id={$periode_id} 
			AND	karyawan_id	= {$karyawan_id} 
			AND	komponen_id	= {$komponen_id} 
			";
		$result = my_query($query);
		$row = my_fetch_array($result);
		$nominal =  $row['kalkulasi'];
		if((int) $nominal == 0)return false;

	
	$rapel_cuti = 0.003 * get_gapok_rapel($karyawan_id, $periode_gaji_id);
	 
	
	$datas = array(
		'rapel_id'	=>my_type_data_int($rapel_id),
		'periode_prev_id'=>my_type_data_int($periode_id),
		'komponen_id'=>my_type_data_int($komponen_id),
		'karyawan_id'=>my_type_data_int($karyawan_id),
		'nominal'=>my_type_data_str(round($rapel_cuti) ),
	); 
	return my_insert_record( 'pr_rapel_komponen_karyawan' , $datas);
}

/* FORMULA IURAN JKK */
function pr_rapel_iuran_jkk($karyawan_id ,$komponen_id, $periode_id , $rapel_id , $periode_gaji_id){
	 	
		$query = "SELECT kalkulasi FROM pr_gaji_komponen 
			WHERE periode_id={$periode_id} 
			AND	karyawan_id	= {$karyawan_id} 
			AND	komponen_id	= {$komponen_id} 
			";
		$result = my_query($query);
		$row = my_fetch_array($result);
		$nominal =  $row['kalkulasi'];
		if((int) $nominal == 0)return false;

	
	$rapel_cuti = 0.003 * get_gapok_rapel($karyawan_id, $periode_gaji_id);
	 
	
	$datas = array(
		'rapel_id'	=>my_type_data_int($rapel_id),
		'periode_prev_id'=>my_type_data_int($periode_id),
		'komponen_id'=>my_type_data_int($komponen_id),
		'karyawan_id'=>my_type_data_int($karyawan_id),
		'nominal'=>my_type_data_str(round($rapel_cuti) ),
	); 
	return my_insert_record( 'pr_rapel_komponen_karyawan' , $datas);
}
/* FORMULA IURAN JKK */
function pr_rapel_iuran_mitra_krakatau($karyawan_id ,$komponen_id, $periode_id , $rapel_id , $periode_gaji_id){
	 	
		$query = "SELECT kalkulasi FROM pr_gaji_komponen 
			WHERE periode_id={$periode_id} 
			AND	karyawan_id	= {$karyawan_id} 
			AND	komponen_id	= {$komponen_id} 
			";
		$result = my_query($query);
		$row = my_fetch_array($result);
		$nominal =  $row['kalkulasi'];
		if((int) $nominal == 0)return false;

	
	$rapel_iuran = 0.15 * get_gapok_rapel($karyawan_id, $periode_gaji_id);
	 
	
	$datas = array(
		'rapel_id'	=>my_type_data_int($rapel_id),
		'periode_prev_id'=>my_type_data_int($periode_id),
		'komponen_id'=>my_type_data_int($komponen_id),
		'karyawan_id'=>my_type_data_int($karyawan_id),
		'nominal'=>my_type_data_str(floor($rapel_iuran) ),
	); 
	return my_insert_record( 'pr_rapel_komponen_karyawan' , $datas);
}

function pr_rapel_iuran_tkk_bsm($karyawan_id ,$komponen_id, $periode_id , $rapel_id , $periode_gaji_id){
	 	
		$query = "SELECT kalkulasi FROM pr_gaji_komponen 
			WHERE periode_id={$periode_id} 
			AND	karyawan_id	= {$karyawan_id} 
			AND	komponen_id	= {$komponen_id} 
			";
		$result = my_query($query);
		$row = my_fetch_array($result);
		$nominal =  $row['kalkulasi'];
		if((int) $nominal == 0)return false;

	
	$rapel_iuran = 0.028 * get_gapok_rapel($karyawan_id, $periode_gaji_id);
	 
	
	$datas = array(
		'rapel_id'	=>my_type_data_int($rapel_id),
		'periode_prev_id'=>my_type_data_int($periode_id),
		'komponen_id'=>my_type_data_int($komponen_id),
		'karyawan_id'=>my_type_data_int($karyawan_id),
		'nominal'=>my_type_data_str(floor($rapel_iuran) ),
	); 
	return my_insert_record( 'pr_rapel_komponen_karyawan' , $datas);
}

function pr_rapel_iuran_tkk_organik($karyawan_id ,$komponen_id, $periode_id , $rapel_id , $periode_gaji_id){
	 	
		$query = "SELECT kalkulasi FROM pr_gaji_komponen 
			WHERE periode_id={$periode_id} 
			AND	karyawan_id	= {$karyawan_id} 
			AND	komponen_id	= {$komponen_id} 
			";
		$result = my_query($query);
		$row = my_fetch_array($result);
		$nominal =  $row['kalkulasi'];
		if((int) $nominal == 0)return false;

	
	$rapel_iuran = 0.028 * get_gapok_rapel($karyawan_id, $periode_gaji_id);
	 
	
	$datas = array(
		'rapel_id'	=>my_type_data_int($rapel_id),
		'periode_prev_id'=>my_type_data_int($periode_id),
		'komponen_id'=>my_type_data_int($komponen_id),
		'karyawan_id'=>my_type_data_int($karyawan_id),
		'nominal'=>my_type_data_str(floor($rapel_iuran) ),
	); 
	return my_insert_record( 'pr_rapel_komponen_karyawan' , $datas);
}

/*FORMULA POTONGAN PENSIUN MITRA KRAKATAU */
function pr_rapel_potongan_mitra_krakatau($karyawan_id ,$komponen_id, $periode_id , $rapel_id , $periode_gaji_id){
	 	
		$query = "SELECT kalkulasi FROM pr_gaji_komponen 
			WHERE periode_id={$periode_id} 
			AND	karyawan_id	= {$karyawan_id} 
			AND	komponen_id	= {$komponen_id} 
			";
		$result = my_query($query);
		$row = my_fetch_array($result);
		$nominal =  $row['kalkulasi'];
		if((int) $nominal == 0)return false;

	
	$rapel_cuti = 0.05 * get_gapok_rapel($karyawan_id, $periode_gaji_id);
	 
	
	$datas = array(
		'rapel_id'	=>my_type_data_int($rapel_id),
		'periode_prev_id'=>my_type_data_int($periode_id),
		'komponen_id'=>my_type_data_int($komponen_id),
		'karyawan_id'=>my_type_data_int($karyawan_id),
		'nominal'=>my_type_data_str(round($rapel_cuti) ),
	); 
	return my_insert_record( 'pr_rapel_komponen_karyawan' , $datas);
}
/*FORMULA POTONGAN TKK BSM */
function pr_rapel_potongan_tkk_bsm($karyawan_id ,$komponen_id, $periode_id , $rapel_id , $periode_gaji_id){
	 	
		$query = "SELECT kalkulasi FROM pr_gaji_komponen 
			WHERE periode_id={$periode_id} 
			AND	karyawan_id	= {$karyawan_id} 
			AND	komponen_id	= {$komponen_id} 
			";
		$result = my_query($query);
		$row = my_fetch_array($result);
		$nominal =  $row['kalkulasi'];
		if((int) $nominal == 0)return false;

	
	$rapel_cuti = 0.015 * get_gapok_rapel($karyawan_id, $periode_gaji_id);
	 
	
	$datas = array(
		'rapel_id'	=>my_type_data_int($rapel_id),
		'periode_prev_id'=>my_type_data_int($periode_id),
		'komponen_id'=>my_type_data_int($komponen_id),
		'karyawan_id'=>my_type_data_int($karyawan_id),
		'nominal'=>my_type_data_str(round($rapel_cuti) ),
	); 
	return my_insert_record( 'pr_rapel_komponen_karyawan' , $datas);
}
/*FORMULA POTONGAN TKK Kayr Organik */
function pr_rapel_potongan_kary_organik($karyawan_id ,$komponen_id, $periode_id , $rapel_id , $periode_gaji_id){
	 	
		$query = "SELECT kalkulasi FROM pr_gaji_komponen 
			WHERE periode_id={$periode_id} 
			AND	karyawan_id	= {$karyawan_id} 
			AND	komponen_id	= {$komponen_id} 
			";
		$result = my_query($query);
		$row = my_fetch_array($result);
		$nominal =  $row['kalkulasi'];
		if((int) $nominal == 0)return false;

	
	$rapel_cuti = 0.015 * get_gapok_rapel($karyawan_id, $periode_gaji_id);
	 
	
	$datas = array(
		'rapel_id'	=>my_type_data_int($rapel_id),
		'periode_prev_id'=>my_type_data_int($periode_id),
		'komponen_id'=>my_type_data_int($komponen_id),
		'karyawan_id'=>my_type_data_int($karyawan_id),
		'nominal'=>my_type_data_str(round($rapel_cuti) ),
	); 
	return my_insert_record( 'pr_rapel_komponen_karyawan' , $datas);
}